﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MAP;
/// <summary>
/// Summary description for CalculateBestPosition
/// </summary>
public static class CalculateBestPosition
{

    private static RECTF hasVisitedRect = new RECTF();
    private static RECTF comparedRect = new RECTF();
    private static List<Building> buildInfo = new List<Building>();
    private static List<Building> retBuild = new List<Building>();

    public static List<Building> GetBestArea(RECTF rect)
    {
        List<Building> currentVisit = QueryEntrance.queryByPosition(
            rect.centerX,
            rect.centerY,
            rect.xRadius,
            rect.yRadius);

        try
        {
            int left = Math.Max((int)rect.left, (int)hasVisitedRect.left);
            int top = Math.Max((int)rect.top, (int)hasVisitedRect.top);
            int right = Math.Min((int)(rect.left + rect.length), (int)(hasVisitedRect.left + hasVisitedRect.length));
            int bottom = Math.Min((int)(rect.top + rect.height), (int)(hasVisitedRect.top + hasVisitedRect.height));

            foreach (Building build in currentVisit)
            {
                int X = (int)build.Position.X;
                int Y = (int)build.Position.Y;
                if (X > left && X < right && Y < top && Y > bottom)
                {
                    build.Clear();
                    build.setVisited(true);
                    hasVisitedRect = rect;
                    return currentVisit;
                }
            }
        }
        catch (System.Exception e)
        {
            return null;
        }
        return currentVisit;
    }
}
//    /// <summary>
//    /// 将整个缓存里面的坐标进行比对,选出需要返回的
//    /// </summary>
//    /// <returns></returns>
//    public static List<Building> getUsefulArea()
//    {
//        retBuild.Clear();
//        int i = 0;
//        try
//        {
//            while (buildInfo[i].getBuildPos().X < currentVisitedRect.left + currentVisitedRect.length
//              && buildInfo[i].getBuildPos().X > currentVisitedRect.left
//              && buildInfo[i].getBuildPos().Y < currentVisitedRect.top + currentVisitedRect.height
//              && buildInfo[i].getBuildPos().Y > currentVisitedRect.top)
//            {
//                retBuild.Add(buildInfo[i]);
//                if (i < buildInfo.Count)
//                    i++;
//                else
//                    break;
//            }
//        }
//        catch (System.Exception e)
//        {
//            return null;
//        }
       
//        return retBuild;
//    }
//}
